<template>
  <div>
    <el-table
      :data="tableData"
      :span-method="objectSpanMethod"
      border
      :header-cell-style="{ textAlign: 'center', backgroundColor: '#F5F7FA' }"
    >
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="分配类型" align="center" prop="allocType" />
      <el-table-column label="商品类型" align="center" prop="productType" />
      <el-table-column label="商品编码" align="center" prop="productCode" />
      <el-table-column label="上架ID" align="center" prop="shelfId" />
      <el-table-column
        label="上架数量"
        align="center"
        prop="productStockCount"
      />
      <el-table-column label="店铺" align="center" prop="storeName" />
      <el-table-column label="数量" align="center" prop="quantityCount" />
      <el-table-column label="退货比例" align="center" prop="ratio" />
      <el-table-column
        label="计算分配数量"
        align="center"
        prop="allAllocationCount"
      />
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 存放所有的表头 一定要与tableData一致
      colFields: [
        "id",
        "allocType",
        "productType",
        "productCode",
        "shelfId",
        "productStockCount",
        "storeName",
        "quantityCount",
        "ratio",
        "allAllocationCount",
      ],
      spanArr: [], //存储合并单元格的开始位置
      // 表格数据
      tableData: [
        {
          id: 1,
          allocType: "ByOrder",
          productType: "Good",
          productCode: "TK269452M",
          shelfId: 4,
          productStockCount: 1,
          storeName: "vfu运动旗舰店",
          quantityCount: 11,
          ratio: "91.67%",
          allAllocationCount: 1,
        },
        {
          id: 1,
          allocType: "ByOrder",
          productType: "Good",
          productCode: "TK269452M",
          shelfId: 4,
          productStockCount: 1,
          storeName: "VFU-抖音小店",
          quantityCount: 1,
          ratio: "8.33%",
          allAllocationCount: 0,
        },
      ],
    };
  },
  methods: {
    /**
     * 分析每一列，找出相同的
     * @param data
     */
    getSpanArr() {
      for (let i = 0; i < this.tableData.length; i++) {
        let row = i;
        // let col = i % this.colCount;
        if (row === 0) {
          // i 表示行 j表示列
          for (let j = 0; j < this.colFields.length; j++) {
            this.spanArr[i * this.colFields.length + j] = {
              rowspan: 1,
              colspan: 1,
            };
          }
        } else {
          for (let j = 0; j < this.colFields.length; j++) {
            // 当前和上一次的一样
            //  合并所有列的相同数据单元格
            if (
              this.colFields[j] == "id" ||
              this.colFields[j] == "allocType" ||
              this.colFields[j] == "productType" ||
              this.colFields[j] == "productCode" ||
              this.colFields[j] == "shelfId" ||
              this.colFields[j] == "productStockCount"
            ) {
              // 指定合并哪些列
              if (this.tableData[row]["id"] !== this.tableData[row - 1]["id"]) {
                // 哪些不合并:School不一样的,不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              } else if (
                this.tableData[row][this.colFields[j]] ===
                this.tableData[row - 1][this.colFields[j]]
              ) {
                let beforeItem =
                  this.spanArr[(row - 1) * this.colFields.length + j];
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1 + beforeItem.rowspan, // 合并几列
                  colspan: 1, // 合并几行
                };
                beforeItem.rowspan = 0;
                beforeItem.colspan = 0;
              } else {
                // rowspan 和 colspan 都为1表格此单元格不合并
                this.spanArr[row * this.colFields.length + j] = {
                  rowspan: 1,
                  colspan: 1,
                };
              }
            }
          }
        }
      }
      // 对数据进行倒序
      let stack = [];
      for (let i = 0; i < this.colFields.length; i++) {
        for (let j = 0; j < this.tableData.length; j++) {
          // console.log("i=" + i + " j=" + j);
          // i 表示列 j表示行
          if (j === 0) {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            }
          } else {
            if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
              stack.push(this.spanArr[j * this.colFields.length + i]);
            } else {
              stack.push(this.spanArr[j * this.colFields.length + i]);
              while (stack.length > 0) {
                let pop = stack.pop();
                let len = stack.length;
                this.spanArr[(j - len) * this.colFields.length + i] = pop;
              }
            }
          }
        }
      }
      // console.log(this.spanArr);
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log(this.spanArr[rowIndex * this.colFields.length + columnIndex]);
      return this.spanArr[rowIndex * this.colFields.length + columnIndex];
    },
  },
  mounted() {
    this.getSpanArr();
  },
};
</script>
<style lang="scss" scoped></style>